package ltd.zhaocheng.wm;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * 把爬取到的数据保存到文件的逻辑
 *
 * @author: zhaocheng
 * @TIME: 2020/3/21 21:49
 */
public class SavePipeline implements Pipeline {

    private static final ObjectMapper MAPPER = new ObjectMapper();

    @Override
    public void process(ResultItems resultItems, Task task) {
        Map<String,Object> data = new HashMap<>(9);

        String url = resultItems.getRequest().getUrl();
        data.put("url",url);
        data.put("title",resultItems.get("title"));
        data.put("rent",resultItems.get("rent"));

        String[] types = StringUtils.split(resultItems.get("type"), ' ');
        //租赁方式,户型，如：2室1厅1卫,朝向
        data.put("rentMethod",StringUtils.split(types[0],"：")[1]);
        data.put("houseType",StringUtils.split(types[1],"：")[1]);
        data.put("orientation",StringUtils.split(types[3],"：")[1]);

        String[] infos = StringUtils.split(resultItems.get("info"), ' ');
        //看房时间,楼层
        for (String info : infos) {
            if (StringUtils.startsWith(info,"看房：")){
                data.put("time",StringUtils.split(info,"：")[1]);
            }else if (StringUtils.startsWith(info,"楼层：")){
                data.put("floor",StringUtils.split(info,"：")[1]);
            }
        }
        //图片地址
        String imageUrl = StringUtils.split(resultItems.get("img"),'"')[3];
        //用url中的房源id作为图片的新名称
        String newName = StringUtils.substringBeforeLast(StringUtils.substringAfterLast(url,"/"),".")+".jpg";

        try {
            this.downloadFile(imageUrl,new File("D:\\zc\\temp\\image\\"+newName));
            data.put("image",newName);
            String json = MAPPER.writeValueAsString(data);
            FileUtils.write(new File("D:\\zc\\temp\\data.json"),json+"\n","UTF-8",true);
        } catch (Exception e) {
            e.printStackTrace();
        }
//        resultItems.get("img");
    }

    /**
     * 下载文件
     *
     * @param url  文件url
     * @param dest 目标目录
     * @throws Exception
     */
    public void downloadFile(String url, File dest) throws Exception {
        HttpGet httpGet = new HttpGet(url);
        CloseableHttpResponse response = HttpClientBuilder.create().build().execute(httpGet);
        try {
            FileUtils.writeByteArrayToFile(dest, IOUtils.toByteArray(response.getEntity().getContent()));
        } finally {
            response.close();
        }
    }
}
